'\" et
.TH FDOPEN "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
fdopen
\(em associate a stream with a file descriptor
.SH SYNOPSIS
.LP
.nf
#include <stdio.h>
.P
FILE *fdopen(int \fIfildes\fP, const char *\fImode\fP);
.fi
.SH DESCRIPTION
The
\fIfdopen\fR()
function shall associate a stream with a file descriptor.
.P
The
.IR mode
argument is a character string having one of the following values:
.IP "\fIr\fP\ or\ \fIrb\fP" 14
Open a file for reading.
.IP "\fIw\fP\ or\ \fIwb\fP" 14
Open a file for writing.
.IP "\fIa\fP\ or\ \fIab\fP" 14
Open a file for writing at end-of-file.
.IP "\fIr\fP+\ or\ \fIrb\fP+\ or\ \fIr\fP+\fIb\fP" 14
Open a file for update (reading and writing).
.IP "\fIw\fP+\ or\ \fIwb\fP+\ or\ \fIw\fP+\fIb\fP" 14
Open a file for update (reading and writing).
.IP "\fIa\fP+\ or\ \fIab\fP+\ or\ \fIa\fP+\fIb\fP" 14
Open a file for update (reading and writing) at end-of-file.
.P
The meaning of these flags is exactly as specified in
\fIfopen\fR(),
except that modes beginning with
.IR w
shall not cause truncation of the file.
.P
Additional values for the
.IR mode
argument may be supported by an implementation.
.P
The application shall ensure that the mode of the stream as expressed
by the
.IR mode
argument is allowed by the file access mode of the open file
description to which
.IR fildes
refers. The file position indicator associated with the new stream is
set to the position indicated by the file offset associated with the
file descriptor.
.P
The error and end-of-file indicators for the stream shall be cleared.
The
\fIfdopen\fR()
function may cause the last data access timestamp of the underlying
file to be marked for update.
.P
If
.IR fildes
refers to a shared memory object, the result of the
\fIfdopen\fR()
function is unspecified.
.P
If
.IR fildes
refers to a typed memory object, the result of the
\fIfdopen\fR()
function is unspecified.
.P
The
\fIfdopen\fR()
function shall preserve the offset maximum previously set for the
open file description corresponding to
.IR fildes .
.SH "RETURN VALUE"
Upon successful completion,
\fIfdopen\fR()
shall return a pointer to a stream; otherwise, a null pointer shall be
returned and
.IR errno
set to indicate the error.
.SH ERRORS
The
\fIfdopen\fR()
function shall fail if:
.TP
.BR EMFILE
{STREAM_MAX}
streams are currently open in the calling process.
.P
The
\fIfdopen\fR()
function may fail if:
.TP
.BR EBADF
The
.IR fildes
argument is not a valid file descriptor.
.TP
.BR EINVAL
The
.IR mode
argument is not a valid mode.
.TP
.BR EMFILE
{FOPEN_MAX}
streams are currently open in the calling process.
.TP
.BR ENOMEM
Insufficient space to allocate a buffer.
.LP
.IR "The following sections are informative."
.SH EXAMPLES
None.
.SH "APPLICATION USAGE"
File descriptors are obtained from calls like
\fIopen\fR(),
\fIdup\fR(),
\fIcreat\fR(),
or
\fIpipe\fR(),
which open files but do not return streams.
.SH RATIONALE
The file descriptor may have been obtained from
\fIopen\fR(),
\fIcreat\fR(),
\fIpipe\fR(),
\fIdup\fR(),
\fIfcntl\fR(),
or
\fIsocket\fR();
inherited through
\fIfork\fR(),
\fIposix_spawn\fR(),
or
.IR exec ;
or perhaps obtained by other means.
.P
The meanings of the
.IR mode
arguments of
\fIfdopen\fR()
and
\fIfopen\fR()
differ. With
\fIfdopen\fR(),
open for write (\fIw\fP or \fIw+\fP) does not truncate, and append
(\fIa\fP or \fIa+\fP) cannot create for writing. The
.IR mode
argument formats that include a \fIb\fP are allowed for consistency
with the ISO\ C standard function
\fIfopen\fR().
The \fIb\fP has no effect on the resulting stream. Although not
explicitly required by this volume of POSIX.1\(hy2017, a good implementation of append (\fIa\fP)
mode would cause the O_APPEND flag to be set.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "Section 2.5.1" ", " "Interaction of File Descriptors and Standard I/O Streams",
.IR "\fIfclose\fR\^(\|)",
.IR "\fIfmemopen\fR\^(\|)",
.IR "\fIfopen\fR\^(\|)",
.IR "\fIopen\fR\^(\|)",
.IR "\fIopen_memstream\fR\^(\|)",
.IR "\fIposix_spawn\fR\^(\|)",
.IR "\fIsocket\fR\^(\|)"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "\fB<stdio.h>\fP"
.\"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1-2017, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, 2018 Edition,
Copyright (C) 2018 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
.PP
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
